layui.use(['table', 'admin', 'ax','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;

    laydate.render({
      elem: '#producetime'
    });

    /**
     * 库存管理管理
     */
    var FruitsStorageStock = {
        tableId: "fruitsStorageStockTable"
    };

    /**
     * 初始化表格的列
     */
    FruitsStorageStock.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键id'},
            {field: 'stockstate', sort: true, title: '状态', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'warehouseName', sort: true, title: '仓库名称', style:'font-weight: 600;'},
            {field: 'positionNumber', sort: true, title: '库位', style:'font-weight: 600;'},
            {field: 'goodsId', sort: true, title: '商品id', hide: true},
            {field: 'goodsName', sort: true, title: '商品名称', style:'font-weight: 600;'},
            {field: 'grades', sort: true, title: '等级', style:'font-weight: 600;'},
            {field: 'caseWeightOne', sort: true, title: '每箱重量', style:'font-weight: 600;'},
            /*{field: 'caseSpecs', sort: true, title: '箱规', style:'font-weight: 600;'},*/
            {field: 'caseWeight', sort: true, title: '箱皮', style:'font-weight: 600;', totalRowText: '合计：'},
            {field: 'gweight', sort: true, title: '毛重', style:'font-weight: 600;', totalRow: true},
            {field: 'weight', sort: true, title: '净重', style:'font-weight: 600;', totalRow: true},
            {field: 'nums', sort: true, title: '数量', style:'font-weight: 600;', totalRow: true},
            {field: 'producetime', sort: true, title: '生产日期', style:'font-weight: 600;'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    FruitsStorageStock.search = function () {
        var queryData = {};
        queryData['stockstate'] = $("#stockstate").val();
        queryData['condition'] = $("#condition").val();
        queryData['positionNumber'] = $("#positionNumber").val();
        queryData['grades'] = $("#grades").val();
        queryData['producetime'] = $("#producetime").val();
        table.reload(FruitsStorageStock.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 导出excel按钮
     */
    FruitsStorageStock.exportExcel = function () {
        var checkRows = table.checkStatus(FruitsStorageStock.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + FruitsStorageStock.tableId,
        url: Feng.ctxPath + '/fruitsStorageStock/list',
        page: true,
        limit: 40,
        height: "full-60",
        totalRow: true ,//开启合计行
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: FruitsStorageStock.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        FruitsStorageStock.search();
    });

    // 导出excel
    $('#btnExp').click(function () {
        FruitsStorageStock.exportExcel();
    });

    // 单击行事件
    table.on('row(' + FruitsStorageStock.tableId + ')', function (obj) {
		$('div[lay-id="' + FruitsStorageStock.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
		$('div[lay-id="' + FruitsStorageStock.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6" });//改变当前tr颜色
    });

    // 工具条点击事件
    table.on('tool(' + FruitsStorageStock.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'empty') {
	        var operation = function () {
	            var ajax = new $ax(Feng.ctxPath + "/fruitsStorageStock/empty", function (data) {
	                Feng.success("操作成功!");
	                table.reload(FruitsStorageStock.tableId);
	            }, function (data) {
	                Feng.error("操作失败!" + data.message + "!");
	            });
	            ajax.set("id", data.id);
	            ajax.start();
	        };
	        Feng.confirm("确定损耗吗?", operation);
        } else if (layEvent === 'scrap') {
	        var operation = function () {
	            var ajax = new $ax(Feng.ctxPath + "/fruitsStorageStock/scrap", function (data) {
	                Feng.success("报废成功!");
	                table.reload(FruitsStorageStock.tableId);
	            }, function (data) {
	                Feng.error("报废失败!" + data.message + "!");
	            });
	            ajax.set("id", data.id);
	            ajax.start();
	        };
	        Feng.confirm("确定报废吗?", operation);
        }
    });
});
